using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.EventSystems;

public class UI_ItemSlot_Flask : UI_ItemSlot
{

    [SerializeField] private Sprite defaultSprite;
    protected ItemData_Flask itemFlask;
    public int site;

    public void Start()
    {
        itemImage.sprite = defaultSprite;
    }

    private void OnValidate()
    {
        gameObject.name = site.ToString();
    }

    public override void UpdateSlot(InventoryItem item)
    {
        if (item != null)
        {
            this.item = item;
            this.itemFlask = item.data as ItemData_Flask;
            itemImage.sprite = item.data.icon;
            if (item.sizeStack > 1)
            {
                itemAmountText.text = item.sizeStack.ToString();
            }
            else
            {
                itemAmountText.text = "";
            }
        }
        else
        {
            this.item = null;
            this.itemFlask = null;
            itemAmountText.text = "";
            itemImage.sprite = defaultSprite;
        }
    }

    public void UseFlaskInField()
    {
        if (itemFlask != null)
        {
            InventoryManager.instance.UseItem(item);
            if (item.sizeStack < 1)
            {
                InventoryManager.instance.UnEquipFlask(site);
            }
        }
    }

    public override void OnPointerDown(PointerEventData eventData)
    {
        if (eventData.pointerId == -1)
        {
            UseFlaskInField();

        } else if (eventData.pointerId == -2) 
        {
            InventoryManager.instance.UnEquipFlask(site);
            UpdateSlot(null);
        }
    }
}
